import { plotDataMatching } from "./plotDataMatching";
import { widget, graphicLayer, store, mars3d } from "@/utils/global";
import { ElMessage, ElMessageBox } from "element-plus";
import { removeUserStyle } from "@/api/tool";
import { getDrawData } from "@/api/draw";
/**
 * 点击绘制列表,开始绘制地图标绘
 * @param data
 */
export const itemClick = (data: any, pageData: any) => {
    if (pageData.isEditor) {
        ElMessage.error("请先结束编辑状态!");
        return;
    }
    widget.drawItemData = data;
    // widget.drawItemData
    if (data.userId) {
        // 获取用户自定义样式数据
        getDrawData({ id: data.drawId }).then((res: any) => {
            let newDraw = res.data;
            if (newDraw) {
                // 清除自定义样式的文本
                let styles: any = JSON.parse(newDraw.style);
                if (styles.label) {
                    if (styles.label.isLength || styles.label.isArea || styles.label.isVolume) {
                        styles.label.text = "";
                    }
                }
                // 绘制自定义样式
                graphicLayer.startDraw({
                    type: newDraw.type,
                    plottingName: newDraw.plottingName, // 标绘显示类型名称
                    plottingType: newDraw.plottingType, // 标绘类型
                    name: newDraw.name, // 标绘样式名称
                    isCustom: 0, // 是否自定义样式
                    folderId: store.treeDefaultKey,
                    number: `${newDraw.type}${new Date().getTime()}`,
                    style: styles,
                });
            } else {
                // ElMessage.error("获取自定义样式失败!");
                ElMessage.error("标绘原始数据已被删除!");
            }
        });
    } else {
        if (data.value.includes("model")) {
            widget.uploadDialog = true;
            // console.log(widget.drawItemData, 111, widget.uploadDialog);
        } else {
            // 绘制默认样式
            widget.addStyle = false;
            plotDataMatching(data);
        }
    }
};
/**
 * 编辑列表时的复选框操作
 * @param data
 */
export const checkboxChange = (data: any) => {
    console.log(data.checked);
    // data.checked = !data.checked;
    // console.log(data);
};
/**
 * 删除列表项
 */
export const removeEditorPunctuationList = (data: any) => {
    // console.log(widget.customStyleItem);
    let list = widget.customStyleItem.filter((item: any) => {
        return item.checked;
    });
    if (list.length == 0) {
        ElMessage.error("请选择要删除的项!");
        return;
    }
    ElMessageBox.confirm("是否删除所选择的自定义样式?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    }).then(() => {
        list.forEach((item: any, i: number) => {
            removeUserStyle({ id: item.id }).then((res: any) => {
                if (i == list.length - 1) {
                    ElMessage.success("删除成功");
                    data.isEditor = false;
                }
                widget.customStyleItem.splice(widget.customStyleItem.indexOf(item), 1);
            });
            // }
        });
    });
};
/**
 * 取消列表编辑
 * @param data
 */
export const cancelEditorPunctuationList = (data: any) => {
    // console.log(data);
    data.isEditor = false;
    let list = widget.customStyleItem;
    list.forEach((item: any) => {
        item.checked = false;
    });
};
/**
 * 新增列表项,带userid,开始绘制地图标绘
 */
export const newItemClick = (data: any, pageData: any) => {
    pageData.dialogVisible = false;
    widget.drawItemData = data;
    widget.addStyle = true;
    store.isCustom = 1;
    plotDataMatching(data);
};
